/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import junit.framework.Assert;
import com.saville.debug.*;
import com.saville.serdes.*;
import com.saville.msgcomp.*;

public class TestMsgComp {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Log.appName = "TestMsgComp";
        
        /* Test String serialization as part of m.obj */
        Msg m = new Msg();
        m.arg1 = 1;
        m.arg2 = 2;
        m.arg3 = 3;
        m.arg4 = 4;
        //m.obj = new StringSer("hi");
        SerdesBinary s = new SerdesBinary();
        m.serialize(s);
        byte b[] = s.data();
        s.init(b, 0, s.size());
        Msg xm = (Msg) s.readObject();
        String str = (String) xm.obj;
        Assert.assertEquals(m.arg1, xm.arg1);
        Assert.assertEquals(m.arg2, xm.arg2);
        Assert.assertEquals(m.arg3, xm.arg3);
        Assert.assertEquals(m.arg4, xm.arg4);
        //Assert.assertEquals(str, "hi");
        
        /* Test String json serialization as part of m.obj */
        SerdesJson sj = new SerdesJson();
        m.serialize(sj);
        b = sj.data();
        sj.init(b);
        xm = (Msg) sj.readObject();
        str = (String) xm.obj;
        Assert.assertEquals(m.arg1, xm.arg1);
        Assert.assertEquals(m.arg2, xm.arg2);
        Assert.assertEquals(m.arg3, xm.arg3);
        Assert.assertEquals(m.arg4, xm.arg4);
        //Assert.assertEquals(str, "hi");
        
        McId mcId = new McId();
        mcId.id = 1;
        mcId.ipAddr = "127.0.0.1";
        mcId.ipPort = 1234;
        mcId.name = "mcid";
        mcId.remote = true;
        mcId.remoteId = 2;
        s.init();
        mcId.serialize(s);
        b = s.data();
        s.init(b, 0, s.size());
        McId xmcId = (McId) s.readObject();
        Assert.assertEquals(mcId.id, xmcId.id);
        Assert.assertEquals(mcId.ipAddr, xmcId.ipAddr);
        Assert.assertEquals(mcId.ipPort, xmcId.ipPort);
        Assert.assertEquals(mcId.name, xmcId.name);
        Assert.assertEquals(mcId.remote, xmcId.remote);
        Assert.assertEquals(mcId.remoteId, xmcId.remoteId);
        
        sj.init();
        mcId.serialize(sj);
        b = sj.data();
        sj.init(b);
        xmcId = (McId) sj.readObject();
        Assert.assertEquals(mcId.id, xmcId.id);
        Assert.assertEquals(mcId.ipAddr, xmcId.ipAddr);
        Assert.assertEquals(mcId.ipPort, xmcId.ipPort);
        Assert.assertEquals(mcId.name, xmcId.name);
        Assert.assertEquals(mcId.remote, xmcId.remote);
        Assert.assertEquals(mcId.remoteId, xmcId.remoteId);
        
        DevId devId = new DevId();
        devId.id = 1;
        devId.urn = "mcid";
        devId.friendlyName = "fn-mcid";
        s.init();
        devId.serialize(s);
        b = s.data();
        s.init(b, 0, s.size());
        DevId xdevId = (DevId) s.readObject();
        Assert.assertEquals(devId.id, xdevId.id);
        Assert.assertEquals(devId.urn, xdevId.urn);
        Assert.assertEquals(devId.friendlyName, xdevId.friendlyName);
        
        sj.init();
        devId.serialize(sj);
        b = sj.data();
        sj.init(b);
        xdevId = (DevId) sj.readObject();
        Assert.assertEquals(devId.id, xdevId.id);
        Assert.assertEquals(devId.urn, xdevId.urn);
        Assert.assertEquals(devId.friendlyName, xdevId.friendlyName);
        Log.print("TestMsgComp");
    }
    

}
